/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method;


import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.entity.entity.ICefData;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.AccessorClassGenContext;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.HashMap;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;
import java.util.Map;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

public class AcsCreateChildGenerator extends ClassMethodGenerator
{

	private final IGspCommonDataType node;
	private java.util.HashMap<String, String> childTypes;
	private AccessorClassGenContext rootContext;
	private static String varChildCode="childCode";

	public AcsCreateChildGenerator(IGspCommonDataType node, HashMap<String, String> childTypes,
			AccessorClassGenContext rootContext) {
		this.node = node;
		this.childTypes = childTypes;
		this.rootContext = rootContext;
	}

	@Override
	protected String getMethodName()
	{
		return "innerCreateChild";
	}

	@Override
	protected TypeInfo getReturnType() {
		return new TypeInfo(ICefData.class.getTypeName());
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		ArrayList<Modifier.ModifierKeyword> list =new ArrayList<>();
		list.add(ModifierKeyword.PROTECTED_KEYWORD);
		return list;
	}

	@Override
	protected boolean getIsOverride() {
		return true;
	}

	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		ArrayList<ParameterInfo> list=new ArrayList<>();
		ParameterInfo parameterInfo=new ParameterInfo();
		parameterInfo.setParamName("childCode");
		parameterInfo.setParamType(new TypeInfo(String.class.getTypeName()));
		list.add(parameterInfo);
		return list;
	}

	@Override
	protected Block buildMethodBody() {
		Block block=ast.newBlock();
		addBlockIfStatements(block);
		addThrowNotSupportChildCodeException(block);
		return block;
	}

	private void addBlockIfStatements(Block block) {
		for(Map.Entry<String,String> item:childTypes.entrySet())
		{
			IfStatement ifStatement=ast.newIfStatement();
			addIfExpression(ifStatement,item);
			addIfThenStatement(ifStatement,item);
			block.statements().add(ifStatement);
		}
	}
	private void addIfExpression(IfStatement ifStatement, Map.Entry<String, String> item)
	{
		MethodInvocation methodInvocation=ast.newMethodInvocation();
		methodInvocation.setExpression(ast.newSimpleName(varChildCode));
		methodInvocation.setName(ast.newSimpleName("equals"));
		methodInvocation.arguments().add(ExpressionUtils.getStringLiteral(ast, item.getKey()));
		ifStatement.setExpression(methodInvocation);
	}

	private void addIfThenStatement(IfStatement ifStatement, Map.Entry<String, String> item)
	{
		Block block =ast.newBlock();
		ReturnStatement returnStatement=ast.newReturnStatement();
		ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
		classInstanceCreation.setType(ast.newSimpleType(ast.newName(item.getValue())));

		addNewAccessorArgs(item, classInstanceCreation);
		returnStatement.setExpression(classInstanceCreation);
		block.statements().add(returnStatement);
		ifStatement.setThenStatement(block);
	}

	private void addNewAccessorArgs(Map.Entry<String, String> item, ClassInstanceCreation classInstanceCreation) {
		CastExpression castExpression=ast.newCastExpression();
		String childDataType = ((IGspCommonObject) node).getBelongModel()
				.findObjectByCode(item.getKey()).getGeneratedEntityClassInfo().getClassFullName();
		castExpression.setType(ast.newSimpleType(ast.newName(childDataType)));
		addCastExpression(item, castExpression);
		classInstanceCreation.arguments().add(castExpression);


	}

	private void addCastExpression(Map.Entry<String, String> item, CastExpression castExpression) {
		MethodInvocation methodInvocation=ast.newMethodInvocation();

		methodInvocation.setName(ast.newSimpleName("createChild"));
		addInnerCreateChildExpression(methodInvocation,item);
		methodInvocation.arguments().add(ast.newSimpleName(varChildCode));
		castExpression.setExpression(methodInvocation);
	}

	private void addInnerCreateChildExpression(MethodInvocation methodInvocation, Map.Entry<String, String> item) {
		MethodInvocation innerInvocation=ast.newMethodInvocation();
		innerInvocation.setName(ast.newSimpleName("getInnerData"));
		methodInvocation.setExpression(innerInvocation);
	}

	private void addThrowNotSupportChildCodeException(Block block)
	{
		addImport(CefException.class.getTypeName());
		ThrowStatement throwStatement = GeneratorUtil.getThrowStatement(ast,"不存在子表",varChildCode,true);
		//setExceptionCode.setClassInstanceCreation(classInstanceCreation,ast,"不存在子表",varChildCode);
//		classInstanceCreation.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(RuntimeException.class).getType(ast));

//		InfixExpression infixExpression=ast.newInfixExpression();
//		infixExpression.setOperator(InfixExpression.Operator.PLUS);
//		StringLiteral stringLiteral=ast.newStringLiteral();
//		stringLiteral.setLiteralValue("不存在子表");
//		infixExpression.setLeftOperand(stringLiteral);
//		infixExpression.setRightOperand(ast.newSimpleName(varChildCode));
//		classInstanceCreation.arguments().add(infixExpression);
		block.statements().add(throwStatement);
	}
}
